# -*- coding: utf-8 -*
'''
    Created by XiaoInk at 2021/10/08 21:39:47
    Gitee: https://gitee.com/XiaoInk
'''

import os

from datetime import datetime

from flask import Flask, request
from flask.json import jsonify
from werkzeug.middleware.proxy_fix import ProxyFix

from app.macros import macros
from app.config import get_config
from app.db import get_mysql, get_redis
from app.api import file
from app.tools import get_wan_ip

app = Flask(__name__, static_folder='data/download', static_url_path='/download')

# 初始化配置
conf = get_config(os.getenv('ENV', 'local'))
# 实例化
mysql_w = get_mysql(**conf.MYSQL_W)
mysql_r = get_mysql(**conf.MYSQL_R)
redis = get_redis(**conf.REDIS)

# 禁止接口输出 json 按键排序
app.config['JSON_SORT_KEYS'] = False

# 设置代理层数, 以便获取真实的客户端IP
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=1)

# 设置允许上传文件大小10M, 默认不限制
app.config['MAX_CONTENT_LENGTH'] = 10 * 1024 * 1024

# 注册蓝图
app.register_blueprint(file.bp, url_prefix='/api/file')


@app.route('/ok')
def ok():
    body = {
        'client_ip': request.remote_addr,
        'server_ip': get_wan_ip(),
        'server_date': datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    }
    return jsonify(code=macros.HTTP.StatusOK, msg='ok', data=body)


if __name__ == '__main__':
    pass
